/*=============================================================================*
 *  Copyright 2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *=============================================================================*/
package topics.impl;

import topics.Topic;
import topics.TopicSpace;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * An implementation of a TopicSpace.
 */
public class TopicSpaceImpl
   implements TopicSpace
{
   private String m_targetNs;
   private String m_name         = "TopicSpace";
   private Map    m_rootTopicMap = new HashMap(  );

   /**
    * Creates a new {@link TopicSpaceImpl} object.
    *
    * @param targetNs DOCUMENT_ME
    */
   public TopicSpaceImpl( String targetNs )
   {
      m_targetNs = ( targetNs != null ) ? targetNs : "";
   }

   /**
    * DOCUMENT_ME
    *
    * @param name DOCUMENT_ME
    */
   public void setName( String name )
   {
      m_name = name;
   }

   /**
    * DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public String getName(  )
   {
      return m_name;
   }

   /**
    * DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public String getTargetNamespace(  )
   {
      return m_targetNs;
   }

   /**
    * DOCUMENT_ME
    *
    * @param name DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public Topic getTopic( String name )
   {
      return (Topic) m_rootTopicMap.get( name );
   }

   /**
    * DOCUMENT_ME
    *
    * @param topic DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public Topic addTopic( Topic topic )
   {
      m_rootTopicMap.put( topic.getName(  ),
                          topic );
      topic.setTopicSpace( this );
      return topic;
   }

   /**
    * DOCUMENT_ME
    *
    * @param name DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public Topic addTopic( String name )
   {
      return addTopic( new TopicImpl( name ) );
   }

   /**
    * DOCUMENT_ME
    *
    * @param name DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public boolean containsTopic( String name )
   {
      return m_rootTopicMap.containsKey( name );
   }

   /**
    * DOCUMENT_ME
    *
    * @param name DOCUMENT_ME
    */
   public void removeTopic( String name )
   {
      m_rootTopicMap.remove( name );
   }

   /**
    * DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public Iterator topicIterator(  )
   {
      return m_rootTopicMap.values(  ).iterator(  );
   }
}